from nicegui import binding, ui

class UIField:
    _value = binding.BindableProperty()
    def __init__(self, default_value, display_name):
        self._value = default_value
        self._display_name = display_name

    @property
    def value(self):
        return self._value
    
    def render(self):
        raise NotImplementedError("Subclasses should implement this method")
    
class RadioField(UIField):
    def __init__(self, display_name, default_value, options):
        super().__init__(default_value, display_name, )
        self.options = options

    def render(self):
        with ui.row():
            ui.label(self._display_name)
            ui.radio(self.options, value=self._value).props('inline').bind_value(self, '_value')

class IntField(UIField):
    def __init__(self, display_name, default_value, min, max,step=1):
        super().__init__(default_value, display_name)
        assert isinstance(default_value, int)

        self._min = min
        self._max = max
        self._step = step

    def render(self):
        with ui.row():
            ui.label(self._display_name)
            slider = ui.slider(min=self._min, max=self._max, value=self._value, step=self._step).bind_value(self, '_value')
            ui.label().bind_text_from(slider, 'value')

class BoolField(UIField):
    def __init__(self, display_name, default_value):
        super().__init__(default_value, display_name)

    def render(self):
        with ui.row():
            ui.label(self._display_name)
            ui.checkbox(value=self._value).bind_value(self, '_value')
